package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkgroud")
public class CheckGroudController {

    @Reference
    private CheckGroupService checkGroupService;
    /**
     *   插入检查组
     *    /checkgroud/add.do?checkitemIds="+this.checkitemIds,this.formData
     */
    @RequestMapping("/add")
   public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        try{
            checkGroupService.add(checkGroup,checkitemIds);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS);
        }catch (Exception e){
          return  new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     *   分页数据
     *  /checkgroud/findPage.do
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.finPage(queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return  pageResult;
}

    /**
     *  根据id回显见检查组基本信息
     * checkgroud/findGroupById?id=5
     */
    @RequestMapping("/findGroupById")
    public Result findGroupById(Integer id){
        try{
          CheckGroup checkGroup= checkGroupService.findGroupById(id);
          return  new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     *  根据检查组id--检查项目勾选
     * /checkgroud/finCheckItemIdByCheckGroup.do?id"+row.id
     */
    @RequestMapping("/finCheckItemIdByCheckGroup")
    public Result finCheckItemIdByCheckGroup(Integer id){
        try{
            List<Integer> listIds= checkGroupService.finCheckItemIdByCheckGroup(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,listIds);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     *   跟新检查组信息--基本信息,勾选检查项目
     *   /checkgroud/edit.do?checkitemIds="+this.checkitemIds,this.formData
     */
    @RequestMapping("/edit")
    public Result edit(Integer[]checkitemIds,@RequestBody CheckGroup checkGroup){
        try{
            checkGroupService.edit(checkitemIds,checkGroup);
            return  new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch (Exception e){
            return  new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }
    /**
     *  删除检查组信息
     * /checkgroud/deleteGroup.do?id="+row.id
     */
    @RequestMapping("/deleteGroup")
    public Result deleteGroup(Integer id){
        try {
            checkGroupService.deleteGroup(id);
            return  new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        } catch (Exception e){
            return  new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }

    /**
     * 查看全部检查组的信息
     *  checkgroud/findAllGroup
     */
    @RequestMapping("/findAllGroup")
    public Result findAllGroup(){
        try{
            List<CheckGroup> listGroup= checkGroupService.findAllGroup();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,listGroup);
        }catch (Exception e){
            return  new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
}
